package cn.dd.crmweb.modules.sys.service.impl;

import cn.dd.crmweb.modules.sys.entity.Role;
import cn.dd.crmweb.modules.sys.entity.RoleMenu;
import cn.dd.crmweb.modules.sys.mapper.RoleMapper;
import cn.dd.crmweb.modules.sys.mapper.RoleMenuMapper;
import cn.dd.crmweb.modules.sys.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMenuMapper roleMenuMapper;


    @Transactional
    @Override
    public void setRoleMenu(String roleId, List<String> menuIds) {
//        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_id",roleId);
//        roleMenuMapper.delete(queryWrapper);

        //先删除当前角色id所有的绑定关系
        roleMenuMapper.deleteByRoleId(roleId);

        //在把前端传过来的菜单id数组绑定到当前这个角色id上去
        for (String menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
    }

    @Override
    public List<String> getRoleMenu(String roleId) {

        return roleMenuMapper.selectByRoleId(roleId);
    }


}
